package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.PageResult;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Setmeal;
import com.xrit.sunnyday.service.SetMealService;
import com.xrit.sunnyday.utils.QiNiuUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

/**
 * 类名称: SetMealController
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/6/13 下午 04:29
 * Version 1.0.0
 */
@RestController
@RequestMapping("/setMeal")
public class SetMealController {
    @Reference
    private SetMealService setMealService;

    /*修改操作*/
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        setMealService.update(setmeal,checkgroupIds);
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /*查询套餐拥有的检查组*/
    @GetMapping("/findSetMealAndCheckGroup")
    public Result findSetMealAndCheckGroup(Integer id){
        Integer[] integers = setMealService.findSetMealAndCheckGroup(id);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,integers);
    }


    /*编辑 的单表查询*/
    @GetMapping("/findById")
    public Result findById(Integer id){
       Setmeal setmeal= setMealService.findById(id);
       return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }


    /*添加套餐*/
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        setMealService.add(setmeal,checkgroupIds);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /*文件上传*/
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
       String fileName= UUID.randomUUID()+extension;
        try {
            QiNiuUtils.uploadViaByte(imgFile.getBytes(),fileName);
            HashMap<String, String> map = new HashMap<>();
            map.put("imgName",fileName);
            map.put("domain",QiNiuUtils.DOMAIN);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);

    }

    /*分页查询*/
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageInfo<Setmeal> page= setMealService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,new PageResult(page.getTotal(),page.getList()));
    }
    /*删除*/
    @PostMapping("/deleteById")
    public Result deleteById(Integer id){
        setMealService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

}
